package com.ruoyi.vote.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.vote.admin.service.IVotePrizeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.vote.admin.domain.bo.VotePrizeBo;
import com.ruoyi.vote.admin.domain.vo.VotePrizeVo;
import com.ruoyi.vote.admin.domain.VotePrize;
import com.ruoyi.vote.admin.mapper.VotePrizeMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 活动 - 奖品管理Service业务层处理
 *
 * @author 筑巢
 * @date 2023-07-17
 */
@RequiredArgsConstructor
@Service
public class VotePrizeServiceImpl implements IVotePrizeService {

    private final VotePrizeMapper baseMapper;

    /**
     * 查询活动 - 奖品管理
     */
    @Override
    public VotePrizeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询活动 - 奖品管理列表
     */
    @Override
    public TableDataInfo<VotePrizeVo> queryPageList(VotePrizeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<VotePrize> lqw = buildQueryWrapper(bo);
        Page<VotePrizeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询活动 - 奖品管理列表
     */
    @Override
    public List<VotePrizeVo> queryList(VotePrizeBo bo) {
        LambdaQueryWrapper<VotePrize> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<VotePrize> buildQueryWrapper(VotePrizeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<VotePrize> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), VotePrize::getName, bo.getName());
        lqw.eq(bo.getPrice() != null, VotePrize::getPrice, bo.getPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getLogo()), VotePrize::getLogo, bo.getLogo());
        lqw.eq(bo.getStatus() != null, VotePrize::getStatus, bo.getStatus());
        lqw.eq(bo.getTicketTime() != null, VotePrize::getTicketTime, bo.getTicketTime());
        return lqw;
    }

    /**
     * 新增活动 - 奖品管理
     */
    @Override
    public Boolean insertByBo(VotePrizeBo bo) {
        VotePrize add = BeanUtil.toBean(bo, VotePrize.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改活动 - 奖品管理
     */
    @Override
    public Boolean updateByBo(VotePrizeBo bo) {
        VotePrize update = BeanUtil.toBean(bo, VotePrize.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(VotePrize entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除活动 - 奖品管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
